package cn.tedu.product.controller

import java.util.List
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import cn.tedu.product.service.ProductService
import cn.tedu.common.pojo.Product
import cn.tedu.common.pojo.SysResult

@RestController
@RequestMapping(Array("product/manage"))
class ProductController {

  @Autowired
  private var ps:ProductService=_

  @RequestMapping(Array("/pageManage"))
  def pageQuery(page: Int, rows: Int) = ps.pageQuery(page, rows)

  @RequestMapping(Array("item/{productId}"))
  def queryById(@PathVariable productId: String) = ps queryById productId

  @RequestMapping(Array("save"))
  def saveProduct(product: Product) = {
    try {
      ps saveProduct product; SysResult ok
    } catch {
      case e: Exception => e.printStackTrace(); SysResult.build(201, e.getMessage, null)
    }
  }

  @RequestMapping(Array("update"))
  def updateProdut(product: Product) = {
    try {
      ps updateProduct product; SysResult ok
    } catch {
      case e: Exception => e.printStackTrace(); SysResult.build(201, e.getMessage, null)
    }
  }

  @RequestMapping(Array("createIndex"))
  def createIndex(indexName: String) = {
    try {
      ps createIndex indexName; SysResult ok
    } catch {
      case e: Exception => e.printStackTrace(); SysResult.build(201, e.getMessage, null)
    }
  }
  @RequestMapping(Array("search"))
  def search(indexName: String, value: String) = ps.search(indexName, value)
}